package com.qlm.tool;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 查快递
 * @author wangzih
 *
 */
public class Delivery {
	//快递单位 默认百世汇通，可以通过快递100查询
	private static String type = "huitongkuaidi";
	
	//快递单号
	private String num = "";
	
	private boolean success = false;
	
	private String time = "";
	
	private String context = "";

	/**
	 * 快递内容
	 */
	private Delivery [] data;
	
	
	public  static Delivery query(String num){
		  Delivery d= new Delivery();
		  String url = "http://www.kuaidi100.com/query?type="+type+"&postid="+num+"&id=1&valicode=&temp=0.3015635129995644";    
	        try {    
	            HttpURLConnection.setFollowRedirects(true);    
	            HttpURLConnection http = (HttpURLConnection) (new URL(url).openConnection());    
	            http.setDoOutput(true);    
	            http.setDoOutput(true);    
	            http.setInstanceFollowRedirects(true);    
	            http.setRequestMethod("GET");    
	            http.setRequestProperty("Connection", "keep-alive");    
	            http.setRequestProperty("X-Requested-With", "XMLHttpRequest");    
	            http.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.92 Safari/537.1 LBBROWSER");    
	            http.setRequestProperty("Accept", "*/*");    
	            http.setRequestProperty("Referer", "http://www.kuaidi100.com/");    
	            http.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");    
	            http.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");    
	            http.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");    
	                
	            //System.out.println("response  is : "+http.getResponseCode()+" "+http.getResponseMessage());    
	            String contentEncoding = http.getContentEncoding();    
//	            System.out.println("response  encoding is : "+contentEncoding);    
	            InputStream  in = null;    
		            if("gzip".equalsIgnoreCase(contentEncoding)){    
		                in = new GZIPInputStream(http.getInputStream());     
		            }else{    
		                in = http.getInputStream();    
		            }
	            try{
		            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		            try{
			            int data = -1;    
			            while((data = in.read())!=-1){    
			                baos.write(data);    
			            }    
		            }finally{
		            	baos.close();
		            }
		            String resp = baos.toString("utf8");  
		            JSONObject parseObject = JSONObject.parseObject(resp);
		            d.parse(parseObject);
	            }finally{
	            	in.close();
	            }
	        } catch (MalformedURLException e) {    
	            e.printStackTrace();    
	        } catch (IOException e) {    
	            e.printStackTrace();    
	        }
			return d;    
	           
	}
	private void parse(JSONObject obj) {
		if(obj.getString("status").equals("200")){
			this.success = true;
			String string = obj.getString("data");
			JSONArray parseArray = JSONArray.parseArray(string);
			int size = parseArray.size();
			this.data = new Delivery[size];
			for (int i = 0; i < size; i++) {
				JSONObject o = parseArray.getJSONObject(i);
				Delivery d = new Delivery();
				String time = o.getString("time");
				String context = o.getString("context");
				d.setContext(context);
				d.setTime(time);
				this.data[i] = d;
			}
		}
		
	}
	public Delivery[] getData() {
		return data;
	}
	public void setData(Delivery[] data) {
		this.data = data;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getContext() {
		return context;
	}
	public void setContext(String context) {
		this.context = context;
	}
}
